﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace TST.SharePointObjects
{
    public class ObjectReference : BaseObject
    {
        private string _objectID;
        private string _url;
        private string _siteTitle;
        private string _siteUrl;
        private string _listTitle;
        private string _listUrl;
        private ObjectReferenceType _type;

        public string ObjectId
        {
            get { return _objectID; }
            set { _objectID = value; }
        }

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        public string SiteTitle
        {
            get { return _siteTitle; }
            set { _siteTitle = value; }
        }

        public string SiteUrl
        {
            get { return _siteUrl; }
            set { _siteUrl = value; }
        }

        public string ListTitle
        {
            get { return _listTitle; }
            set { _listTitle = value; }
        }

        public string ListUrl
        {
            get { return _listUrl; }
            set { _listUrl = value; }
        }

        public ObjectReferenceType ObjectReferenceType
        {
            get { return _type; }
            set { _type = value; }
        }

        public string ReferenceTypeDescription
        {
            get
            {
                return Globals.GetReferenceTypeDescription(ObjectReferenceType);
            }
        }

        public ObjectReference(string id, string name, string objectID, ObjectReferenceType objectReferenceType)
        {
            Id = id;
            _objectID = objectID;
            Name = name;
            _type = objectReferenceType;
        }

        public ObjectReference(DataRow row)
        {
            if (row == null)
                return;

            if (row["ReferenceType"] != null && row.Table.Columns.Contains("ReferenceType"))
            {
                int r = 0;
                if (int.TryParse(row["ReferenceType"].ToString(), out r))
                    ObjectReferenceType = (ObjectReferenceType)Enum.ToObject(typeof(ObjectReferenceType), r);
                else
                    ObjectReferenceType = ObjectReferenceType.Unknown;
            }
            else
            {
                ObjectReferenceType = ObjectReferenceType.Unknown;
            }
            
            if (row["ReferenceID"] != null && row.Table.Columns.Contains("ReferenceID"))
                ObjectId = row["ReferenceID"].ToString();
            
            if (row["ObjectID"] != null && row.Table.Columns.Contains("ObjectID"))
                Id = row["ObjectID"].ToString();

            if (row["Name"] != null && row.Table.Columns.Contains("Name"))
                Name = row["Name"].ToString();

            if (row["Url"] != null && row.Table.Columns.Contains("Url"))
                Url = row["Url"].ToString();

            if (row["SiteTitle"] != null && row.Table.Columns.Contains("SiteTitle"))
                SiteTitle = row["SiteTitle"].ToString();

            if (row["SiteUrl"] != null && row.Table.Columns.Contains("SiteUrl"))
                SiteUrl = row["SiteUrl"].ToString();

            if (row["ListTitle"] != null && row.Table.Columns.Contains("ListTitle"))
                ListTitle = row["ListTitle"].ToString();

            if (row["ListUrl"] != null && row.Table.Columns.Contains("ListUrl"))
                ListUrl = row["ListUrl"].ToString();
        }

    }
}
